package com.zhoug.zmui3.valuepair.align;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;

import com.zhoug.common3.utils.StringUtils;
import com.zhoug.zmui3.R;

import androidx.annotation.Nullable;

/**
 * 键值对水平布局组件:输入框
 *
 * @Author HK-LJJ
 * @Date 2020/3/23
 * @Description
 */
public class ZMUIKeyValueAlignEditText extends ZMUIAbsKeyValueAlignView<EditText> {
    private EditText editText;

    public ZMUIKeyValueAlignEditText(Context context) {
        this(context, null);
    }

    public ZMUIKeyValueAlignEditText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZMUIKeyValueAlignEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public EditText getValueView() {
        if (valueView == null) {
//            return new EditText(getContext());
            ViewGroup root = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.zmui_keyvalue_edittext, null);
            editText = root.findViewById(R.id.editText);
            root.removeView(editText);
            return editText;
        }
        return valueView;
    }

    @Override
    public String getValue() {
        if (valueView != null) {
            String s = valueView.getText().toString();
            return StringUtils.isNullOrEmpty(s) ? null : s;
        }
        return null;
    }

    public EditText getEditText() {
        return editText;
    }

    @Override
    public void setValue(CharSequence value) {
        if (valueView != null) {
            valueView.setText(value);
        }
    }

    @Override
    public void setError(CharSequence error) {
        super.setError(error);
//        editText.setError(error);
    }
}
